package com.neuedu.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.neuedu.mvc.entity.Department;
import com.neuedu.mvc.service.DepartmentService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DepartmentServlet", value = "/dept")
public class DepartmentController extends HttpServlet {

    DepartmentService departmentService = new DepartmentService();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    /**
     *   列表： http://localhost:8080/mvc/dept
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String type = request.getParameter("_type");
            if(type == null || "listpage".equals(type)){
                this.listpage(request,response);
            }else if("list".equals(type)){
                this.list(request,response);
            }else if("ajaxList".equals(type)){
                this.listAjax(request,response);
            }else if("toAdd".equals(type)){
                this.toAdd(request,response);
            }else if("save".equals(type)){
                this.save(request,response);
            }else if("delete".equals(type)){
                this.deleteById(request,response);
            }else if("deleteAjax".equals(type)){
                this.deleteByIdAjax(request,response);
            }

            else if("toEdit".equals(type)){
                this.toEdit(request,response);
            }else if("update".equals(type)){
                this.update(request,response);
            }
        } catch (Exception throwables) {  //
            throwables.printStackTrace();
        }


    }

    private void listpage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/department/department_list_jquery.jsp").forward(request,response);
    }

    /**
     * 执行更新的动作
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SQLException, ClassNotFoundException {

        boolean success = departmentService.update(request);
        if(success){//添加成功的时候
            response.sendRedirect(request.getContextPath()+"/dept?_type=list");
        }else{ //失败
            request.setAttribute("errr","失败的原因");
            request.getRequestDispatcher("/WEB-INF/jsp/department/department_edit.jsp").forward(request,response);
        }
    }

    /**
     * 修改前的 查看信息
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws ServletException
     */
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException, ServletException {

        Department department = departmentService.queryById(request);

        request.setAttribute("dept",department);
        request.getRequestDispatcher("/WEB-INF/jsp/department/department_edit.jsp").forward(request,response);

    }

    /**
     * 删除一条数据
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException {

        boolean success = departmentService.removeById(request);

        response.sendRedirect(request.getContextPath()+"/dept?_type=list");

    }


    /**
     * ajax删除
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void deleteByIdAjax(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException {

        boolean success = departmentService.removeById(request);

        PrintWriter writer = response.getWriter();
        writer.write("{'success':"+success+"}");
        writer.flush();
        writer.close();
    }


    /**
     * 展示添加页面
     * @param request
     * @param response
     */
    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("/WEB-INF/jsp/department/department_add.jsp").forward(request,response);
    }

    /**
     * 保存
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SQLException, ClassNotFoundException {

        boolean success = departmentService.save(request);

        if(success){//添加成功的时候
           response.sendRedirect(request.getContextPath()+"/dept?_type=list");
        }else{ //失败
            request.setAttribute("errr","失败的原因");
            request.getRequestDispatcher("/WEB-INF/jsp/department/department_add.jsp").forward(request,response);
        }

    }




    //列表的方法
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {

        //根据条件查询
        List<Department> departmentList = departmentService.list(request);
        //共享数据
        request.setAttribute("departmentList",departmentList);
        //内部转发给jsp 用于视图渲染
        request.getRequestDispatcher("/WEB-INF/jsp/department/department_list.jsp").forward(request,response);
    }


    //列表的方法
    private void listAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {

        //根据条件查询
        // List<Department> departmentList = departmentService.list(request);
        //带分页的查询
        Map map = departmentService.listPage(request);

        String jsonArray  = JSON.toJSONString(map);
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(jsonArray);

        out.flush();
        out.close();
    }


}
